﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using utils;

namespace dal.cgm_production_dao
{
    [DataContract]
    public class BluetoothTestSensorCodes
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string code { get; set; }
        [DataMember]
        public string batch { get; set; }
        [DataMember]
        public string firmware { get; set; }
        [DataMember]
        public DateTime create_time { get; set; }
    }

    public class BluetoothTestSensorCodesDao
    {
        public static bool Insert(BluetoothTestSensorCodes data)
        {
            bool res = false;
            string sql = @"INSERT INTO bluetooth_test_sensor_codes
           (code,batch,firmware)VALUES
           (@code,@batch,@firmware)";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@code", data.code));
            parameters.Add(new MySqlParameter("@batch", data.batch));
            parameters.Add(new MySqlParameter("@firmware", data.firmware));
            if (MySqlHelper.ExecuteNonQuery(ConfigString.cgm_production, sql, parameters.ToArray()) == 1)
            {
                res = true;
            }

            return res;

        }

        public static BluetoothTestSensorCodes GetCode(string code, string batch, string firmware)
        {
            string sql = @"SELECT id,code,batch,firmware,creat_time FROM bluetooth_test_sensor_codes WHERE code=@code and batch=@batch and firmware=@firmware";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@code", code));
            parameters.Add(new MySqlParameter("@batch", batch));
            parameters.Add(new MySqlParameter("@firmware", firmware));
            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());

            IList<BluetoothTestSensorCodes> list = DataContainerUtil.DataTableToList<BluetoothTestSensorCodes>(ds.Tables[0]);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new BluetoothTestSensorCodes();
            }
        }
    }
}
